//
//  GSCaptchaButton.swift
//  GSCaptchaButton
//
//  Created by Gesen on 15/6/4.
//  Copyright (c) 2015年 Gesen. All rights reserved.
//

import UIKit

public class GSCaptchaButton: UIButton {

    // MARK: Properties
    
    public var maxSecond = 60
    var countdown = false {
        didSet {
            if oldValue != countdown {
                countdown ? startCountdown() : stopCountdown()
            }
        }
    }
    //计时器归零
    var zeroClick: voidBlock?
    
    
    private var second = 0
    private var timer: Timer?
    
    private var timeLabel = UILabel()
    private var normalText: String!
    private var normalTextColor: UIColor!
    private var disabledText: String!
    private var disabledTextColor: UIColor!
    
    // MARK: Life Cycle
    
    deinit {
        countdown = false
    }
    
    // MARK: Setups
    
    private func setupLabel() {
        guard timeLabel.superview == nil else { return }
        
        normalText = title(for: .normal) ?? ""
        disabledText = title(for: .disabled) ?? ""
        normalTextColor = titleColor(for: .normal) ?? .white
        disabledTextColor = titleColor(for: .disabled) ?? .white
        setTitle("", for: .normal)
        setTitle("", for: .disabled)
        timeLabel.frame = bounds
        timeLabel.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        timeLabel.textAlignment = .center
        timeLabel.font = titleLabel?.font
        timeLabel.textColor = normalTextColor
        timeLabel.text = normalText
        addSubview(timeLabel)
    }
    
    // MARK: Private
    
    private func startCountdown() {
        setupLabel()
        second = 60
        updateDisabled()
        
        if timer != nil {
            timer!.invalidate()
            timer = nil
        }
        
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCountdown), userInfo: nil, repeats: true)
    }
    
    private func stopCountdown() {
        timer?.invalidate()
        timer = nil
        updateNormal()
    }
    
    private func updateNormal() {
        isEnabled = true
        timeLabel.textColor = normalTextColor
        timeLabel.text = normalText
    }
    
    private func updateDisabled() {
        isEnabled = false
        timeLabel.textColor = disabledTextColor
        timeLabel.text = disabledText.replacingOccurrences(of: "second", with: "\(second)")
    }
    
    @objc private func updateCountdown() {
        second -= 1
        if second <= 0 {
            countdown = false
            if (self.zeroClick != nil) {
                self.zeroClick!()
            }
        } else {
            updateDisabled()
        }
    }

}



//class GradientButton: UIButton {
//    
//    var gradientLayer: CAGradientLayer {
//        return layer as! CAGradientLayer
//    }
//    
//    override class var layerClass: AnyClass {
//        return CAGradientLayer.self
//    }
//    
//    var colors: [Any]? {
//        didSet {
//            setup(colors: colors)
//        }
//    }
//    
//    /// 构造方法
//    /// - Parameters:
//    ///   - colors: 颜色 CGColorRef
//    ///   - startpoint: 起点
//    ///   - endpoint: 结束点
//    ///   - locations: 位置
//    func setup(colors: [Any]?,
//               startpoint: CGPoint = CGPoint(x: 0.0, y: 0.5),
//               endpoint: CGPoint = CGPoint(x: 1.0, y: 0.5),
//               locations: [NSNumber] = [0, 1.0]) {
//        let gLayer = self.gradientLayer
//        if colors != nil,
//            colors?.isEmpty == false {
//            gLayer.colors = colors
//        }
//        gLayer.startPoint = startpoint
//        gLayer.endPoint = endpoint
//        if locations.count > 0 {
//            gLayer.locations = locations
//        }
//    }
//}
